/**
 * Created by gm on 2018/9/4.
 */
import vuex from 'vuex';
import Vue from 'vue';
import axios from 'axios'
Vue.use(vuex);
export default new vuex.Store({//store对象
  state:{
    tabbar:0,
    toast:{
      showToast:false,
      type:'text',
      width:'7.6em',
      content:'',
      time:700
    },
    loading:{
      show:false
    },
    userInfo:{},
    coinList:[],
  },
  mutations:{
    setCoinList: (state, coinList) => {
      state.coinList = coinList;
    },
    setInfo:(state, info)=>{
      state.userInfo = info;
    }
  },
  actions:{
    currency({ commit }) {
      return new Promise((resolve, reject) => {
        var url = '/api/currency';
        if(process.env.NODE_ENV === 'development'){
          url =  window.g.offline + url;
        }else if(process.env.NODE_ENV === 'production'){
          url =   window.g.ApiUrl + url;
        }
        axios.get(url,{params:{token:localStorage.getItem('token')}}).then(response => {
          var response = response.data;
          if(response.code == 200){
            sessionStorage.setItem('currencyList',JSON.stringify(response.data[0]))
          }
          resolve(response);
        }).catch(error => {
          reject(error)
        })
      })
    },
    accounts({ commit }) {
      return new Promise((resolve, reject) => {
        var url = '/api/accounts';
        if(process.env.NODE_ENV === 'development'){
          url = window.g.offline  + url;
        }else if(process.env.NODE_ENV === 'production'){
          url = window.g.ApiUrl + url;
        }
        axios.get(url,{params:{token:localStorage.getItem('token')}}).then(response => {
          var response = response.data;
          if(response.code == 200){
            sessionStorage.setItem('accountsList',JSON.stringify(response.data[0]))
          }
          resolve(response);
        }).catch(error => {
          reject(error)
        })
      })
    },
    info({ commit }){
     return new Promise((resolve, reject) => {
        var url = '/bigwinner/api/user/getinfo';
        if(process.env.NODE_ENV === 'development'){
          url = window.g.offline + url;
        }else if(process.env.NODE_ENV === 'production'){
          url = window.g.ApiUrl + url;
        }
        axios.get(url,{params:{token:localStorage.getItem('token')}}).then(res=>{
          if(res.data.code == 200){
            this.commit('setInfo',res.data.data.userinfo)
            localStorage.setItem('userinfo',JSON.stringify(res.data.data.userinfo));
          }
          resolve(res);
        }).catch(error=>{
          reject(error)
        })
     })
    },
  }
})
